<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> 15 列表新增</title>
    <script src="./js/vue.js"></script>
    <style>
        .table {
            width: 400px;
            margin: 50px auto;
        }

        .table .tr {
            width: 400px;
        }

        .table .tr input {
            width: 100px;
        }

        .table .tr .item {
            width: 100px;
            height: 40px;
            line-height: 40px;
            float: left;
            background-color: darkblue;
            color: darkorange;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="table">
            <div class="tr">
                <span>用户名</span>
                <input type="text" v-model="username">
                <span>年龄</span>
                <input type="text" v-model="userage">

                <button @click="add">新增</button>

            </div>
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">年龄</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list" :key="item.id">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userage}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>
        </div>

    </div>


</body>

<script>

    new Vue({
        el: "#app",
        data: {
            list: [{ id: 1, username: "sean", userage: 18 }, { id: 2, username: "sean", userage: 18 }, { id: 3, username: "sean", userage: 18 }],
            username: "",
            userage: "",
        },
        computed: {

        },
        methods: {
            del(id) {
                // alert(id)
                this.list = this.list.filter(item => item.id != id)

            },
            add() {



                
                let id = 1;
                if (this.list.length) {
                    id = this.list.map(item => item.id).sort((a, b) => b - a)[0] + 1;
                }

                this.list.push({
                    id: id,
                    username: this.username,
                    userage: this.userage,
                })

            }

        },
        watch: {

        }

    })
</script>

</html>